<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="选择团队成员" v-if="!model.partyId" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="list_multi" v-decorator="['partyIds', validatorRules.partyIds]" :trigger-change="true" 
otherField="otherField"
                             dictUrl="listPersonBySuperiorId"  :placeholder="'请选择团队成员'"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="权限" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['isReadWrite', validatorRules.isReadWrite]" :trigger-change="true" 
dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId =ROLE_CUST" 
:placeholder="'请选择权限'"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="同时添加至" v-if="!model.partyId&&source=='customer'" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-multi-select-tag type="checkbox" v-decorator="['trunkIds']" :trigger-change="true" :options="trunkList"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JMultiSelectTag from '@/components/dict/JMultiSelectTag'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
export default {
  name: 'CrmContactsModal',
  components: {
    JDate,
    JMultiSelectTag,
    JDictSelectTag,
   JSearchSelectTag,
  },
  props: {
    objectId: {
      type: String,
      default: ''
    },
    source: {
      type: String,
      default: ''
    },
  },
  data() {
    return {
      trunkList:'[{ "value":"business", "text": "商机"},{ "value":"contract", "text": "合同"}]',
      disableSubmit: false,
      form: this.$form.createForm(this),
      title: '操作',
      width: 550,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        partyIds: {rules: [{required: true, message: '请选择团队成员!'}]},
        isReadWrite: {rules: [{required: true, message: '请选择权限!'}]},
      },
      url: {
        add: '/online/special/api/createCrmCustomerRoleMany',
        edit: '/online/special/api/updateCrmCustomerRole',
      },
    }
  },
  created() {
  },
  methods: {
    add() {
      this.edit({})
    },
    async edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if(this.objectId){
        this.model.objectId = this.objectId;
        this.model.source = this.source;
      }
      if (record.partyId) {
        this.model = Object.assign({}, record)
      }

      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'partyId',
            'objectId',
            'typeId',
            'description',
            'isReadWrite'
          )
        )
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.partyId) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'partyId',
          'objectId',
          'typeId',
          'description',
          'isReadWrite'
        )
      )
    },
    onChange(){
      console.log(e)
    }
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-modal-mask {
  z-index: 98;
  background-color: rgb(245, 246, 249);
}
/deep/ .ant-modal-wrap {
  z-index: 99;
}
/deep/ .ant-modal-title {
  color: #333;
  font-weight: 700;
  font-size: 17px;
}
</style>